package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import model.DatabaseConnectParameter;
import model.DatabaseJsonModel;
import service.impl.DataHandleServiceImpl;
import service.impl.DatabaseConnectServiceImpl;
import service.impl.ExcelHandleServiceImpl;
import tool.ExcelWorkBook;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @author： 杨浩然
 * @date： 2023-02-01 14:59
 */
public class DataBaseControlle {


    public static void main(String[] args) {

//        String json="{\n" +
//                "    \"databaseName\":\"广东省公房\",\n" +
//                "    \"databaseType\":\"ORACLE\",\n" +
//                "    \"url\":\"192.168.10.60:1521:ihap2\",\n" +
//                "    \"schemaPattern\":\"BGYF_JSJ3\",\n" +
//                "    \"username\":\"bgyf_jsj3\",\n" +
//                "    \"password\":\"bgyf\",\n" +
//                "    \"databaseContrast\":[\n" +
//                "        {\n" +
//                "            \"databaseName\":\"广东省公房\",\n" +
//                "            \"databaseType\":\"ORACLE\",\n" +
//                "            \"url\":\"192.168.10.60:1521:ihap2\",\n" +
//                "            \"schemaPattern\":\"BGYF_SX_CS\",\n" +
//                "            \"username\":\"BGYF_SX_CS\",\n" +
//                "            \"password\":\"BGYF_SX_CS\",\n" +
//                "            \"fromContrast\":{\n" +
//                "                 \t\"COURTYARD\":\"COURTYARD\",\n" +
//                "                    \"BUILDING\":\"BUILDING\",\n" +
//                "                    \"FLOOR\":\"FLOOR\",\n" +
//                "                    \"APROOM\":\"APROOM\",\n" +
//                "                    \"STAFFDETAIL\":\"STAFFDETAIL\",\n" +
//                "                    \"COURTYARDOWNUNIT\":\"COURTYARDOWNUNIT\",\n" +
//                "                    \"COURTYARDOWNUNIT_ZWLIST\":\"COURTYARDOWNUNIT_ZWLIST\"\n" +
//                "            }\n" +
//                "        }\n" +
//                "    ]\n" +
//                "}";
//        Scanner scanner=new Scanner(System.in,"GBK");
        Scanner scanner=new Scanner(System.in,"GBK");
        String temp="";
//        while(scanner.hasNext()) {
//             temp += scanner.nextLine();
////            System.out.println(temp);
//        }
        temp = scanner.nextLine();
//        try {
//            temp=new String(scanner.nextLine().getBytes("utf-8"),"GBK");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
//        System.out.println(temp);

        DataBaseControlle dataBaseControlle=new DataBaseControlle();
//        dataBaseControlle.startHandle(json,"C:\\Users\\86183\\Desktop");
        dataBaseControlle.startHandle(temp,"C:\\yydatabase");
    }


    public void startHandle(String json,String path)
    {
        //将json转为model
        List<DatabaseJsonModel> datajson = DataBaseControlle.jsonModelCreate(json);
        System.out.println(datajson);
        System.out.println("完成解析");

        //数据库连接查询信息
        DatabaseConnectServiceImpl databaseConnectService = new DatabaseConnectServiceImpl();
        List<DatabaseJsonModel> dataConnect = databaseConnectService.loadDatabaseJsonModel(datajson);
        //数据处理对照
        DataHandleServiceImpl dataHandleService = new DataHandleServiceImpl();
        List<DatabaseJsonModel> datahandle = dataHandleService.findDataBaseFroms(dataConnect);
        //excel导出
        ExcelHandleServiceImpl excelHandleService = new ExcelHandleServiceImpl();
        ExcelWorkBook excelWorkBook = excelHandleService.excelHandle(datahandle);
//        excelHandleService.excelExportFile("C:\\Users\\86183\\Desktop",excelWorkBook);
        //文件导出
        excelHandleService.excelExportFile(path,excelWorkBook);
    }

    public static List<DatabaseJsonModel> jsonModelCreate(String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        //标准库的数据连接信息
        String url = jsonObject.getString("url");
        String schemaPattern = jsonObject.getString("schemaPattern");
        String databaseName = jsonObject.getString("databaseName");
        String databaseType = jsonObject.getString("databaseType");
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        JSONArray databaseContrastList = JSON.parseArray(jsonObject.getString("databaseContrast"));
        //标准库打印对照 放在首页
        Set<String> fromStandard = new HashSet<String>();
        DatabaseJsonModel djmtop = new DatabaseJsonModel();
        djmtop.setNum(0);
        djmtop.setDatabaseName(databaseName+"(标准)");
        djmtop.setDcpStandard(DatabaseConnectParameter.Create(
                databaseType,
                fromStandard,
                url,
                schemaPattern,
                username,
                password));

        List<DatabaseJsonModel> databaseJsonModelList = new ArrayList<DatabaseJsonModel>();
        databaseJsonModelList.add(djmtop);

        for (int i = 0; i < databaseContrastList.size(); i++) {
            //对照数据库信息
            JSONObject dbc = databaseContrastList.getJSONObject(i);
            DatabaseJsonModel databaseJsonModel = new DatabaseJsonModel();
            databaseJsonModel.setNum(i + 1);
            databaseJsonModel.setDatabaseName(dbc.getString("databaseName"));
            //具体需要对照的表
            JSONObject fromContrast = dbc.getJSONObject("fromContrast");
            databaseJsonModel.setFrombinding(fromContrast.getInnerMap());
            Set<String> listStandard = new HashSet<String>();
            Set<String> listContrast = new HashSet<String>();
            //fromContrast信息里，key为当前表，value为标准表
            for (String key :
                    fromContrast.keySet()) {
                listContrast.add(key);
                listStandard.add(fromContrast.getString(key));
            }

            databaseJsonModel.setDcpStandard(DatabaseConnectParameter.Create(
                    databaseType,
                    listStandard,
                    url,
                    schemaPattern,
                    username,
                    password));
            databaseJsonModel.setDcpContrast(DatabaseConnectParameter.Create(
                    dbc.getString("databaseType"),
                    listContrast,
                    dbc.getString("url"),
                    dbc.getString("schemaPattern"),
                    dbc.getString("username"),
                    dbc.getString("password")));

            //记录标准表都记录了那些字段
            fromStandard.addAll(listStandard);


            databaseJsonModelList.add(databaseJsonModel);
        }
        return databaseJsonModelList;
    }


    //测试
//    public static void main(String[] args) {
//
////        List<DatabaseJsonModel> datajson = DataBaseControlle.jsonModelCreate("{\n" +
////                "\"databaseName\":\"深圳市办公房\",\n" +
////                "\"databaseType\":\"DM\",\n" +
////                "\"url\":\"14.18.82.151:5236/ibasem_jgswj\",\n" +
////                "\"username\":\"IBASEM_BGYF\",\n" +
////                "\"password\":\"southgissz\",\n" +
////                "\"databaseContrast\":\n" +
////                "[\n" +
////                "{\n" +
////                "\"databaseName\":\"东莞市办公房\",\n" +
////                "\"databaseType\":\"K8S\",\n" +
////                "\"url\":\"192.168.10.15:54321/ibasem_jgswj\",\n" +
////                "\"username\":\"ibasem_bgyf\",\n" +
////                "\"password\":\"south\",\n" +
////                "\"fromContrast\":{\n" +
////                "\"bgyf_courtyard\":\"bgyf_courtyard\",\n" +
////                "\"BGYF_BUILDING\":\"BGYF_BUILDING\",\n" +
////                "\"BGYF_FLOOR\":\"BGYF_FLOOR\",\n" +
////                "\"BGYF_APROOM\":\"BGYF_APROOM\",\n" +
////                "\"BGYF_STAFFDETAIL\":\"BGYF_STAFFDETAIL\",\n" +
////                "\"BGYF_STAFFDETAIL\":\"BGYF_测试标准库STAFFDETAIL\",\n" +
////                "\"BGYF_测试当前STAFFDETAIL\":\"BGYF_STAFFDETAIL\"\n" +
////                "}\n" +
////                "}\n" +
////                "]\n" +
////                "}");
//
//                String json="      {\n" +
//                        "        \"databaseName\":\"深圳市办公房\",\n" +
//                        "            \"databaseType\":\"DM\",\n" +
//                        "            \"url\":\"14.18.82.151:5236/ibasem_jgswj\",\n" +
//                        "            \"username\":\"IBASEM_BGYF\",\n" +
//                        "            \"password\":\"southgissz\",\n" +
//                        "            \"databaseContrast\":[\n" +
//                        "        {\n" +
//                        "            \"databaseName\":\"东莞市办公房\",\n" +
//                        "                \"databaseType\":\"K8S\",\n" +
//                        "                \"url\":\"192.168.10.15:54321/ibasem_jgswj\",\n" +
//                        "                \"username\":\"ibasem_bgyf\",\n" +
//                        "                \"password\":\"south\",\n" +
//                        "                \"fromContrast\":{\n" +
//                        "                    \"bgyf_courtyard\":\"bgyf_courtyard\",\n" +
//                        "                    \"BGYF_BUILDING\":\"BGYF_BUILDING\",\n" +
//                        "                    \"BGYF_FLOOR\":\"BGYF_FLOOR\",\n" +
//                        "                    \"BGYF_APROOM\":\"BGYF_APROOM\",\n" +
//                        "                    \"BGYF_STAFFDETAIL\":\"BGYF_STAFFDETAIL\",\n" +
//                        "                    \"BGYF_COURTYARDOWNUNIT\":\"BGYF_COURTYARDOWNUNIT\",\n" +
//                        "                    \"BGYF_COURTYARDOWNUNIT_ZWLIST\":\"BGYF_COURTYARDOWNUNIT_ZWLIST\"\n" +
//                        "\n" +
//                        "        }\n" +
//                        "        }\n" +
//                        "    ]\n" +
//                        "    }";
//
////        String json="    {\n" +
////                "        \"databaseName\":\"东莞市办公房\",\n" +
////                "            \"databaseType\":\"K8S\",\n" +
////                "            \"url\":\"192.168.10.15:54321/ibasem_jgswj\",\n" +
////                "            \"username\":\"ibasem_bgyf\",\n" +
////                "            \"password\":\"south\",\n" +
////                "            \"databaseContrast\":[\n" +
////                "        {\n" +
////                "            \"databaseName\":\"深圳市办公房\",\n" +
////                "                \"databaseType\":\"DM\",\n" +
////                "                \"url\":\"14.18.82.151:5236/ibasem_jgswj\",\n" +
////                "                \"username\":\"IBASEM_BGYF\",\n" +
////                "                \"password\":\"southgissz\",\n" +
////                "                \"fromContrast\":{\n" +
////                "            \"bgyf_courtyard\":\"bgyf_courtyard\",\n" +
////                "                    \"BGYF_BUILDING\":\"BGYF_BUILDING\",\n" +
////                "                    \"BGYF_FLOOR\":\"BGYF_FLOOR\",\n" +
////                "                    \"BGYF_APROOM\":\"BGYF_APROOM\",\n" +
////                "                    \"BGYF_STAFFDETAIL\":\"BGYF_STAFFDETAIL\"\n" +
////                "        }\n" +
////                "        }\n" +
////                "    ]\n" +
////                "    }";
//
//        List<DatabaseJsonModel> datajson = DataBaseControlle.jsonModelCreate(json);
//
//        System.out.println(datajson);
//        DatabaseConnectServiceImpl databaseConnectService = new DatabaseConnectServiceImpl();
//        List<DatabaseJsonModel> dataConnect = databaseConnectService.loadDatabaseJsonModel(datajson);
//        DataHandleServiceImpl dataHandleService = new DataHandleServiceImpl();
//        List<DatabaseJsonModel> datahandle = dataHandleService.findDataBaseFroms(dataConnect);
//        ExcelHandleServiceImpl excelHandleService = new ExcelHandleServiceImpl();
//        ExcelWorkBook excelWorkBook = excelHandleService.excelHandle(datahandle);
//        excelHandleService.excelExportFile("C:\\Users\\86183\\Desktop",excelWorkBook);
//
////        Map<String, DatabaseFromColumn> dataBaseFrom = databaseConnect.findDataBaseFrom("K8S", "bgyf_courtyard", "192.168.10.15:54321/ibasem_jgswj", "ibasem_bgyf", "south");
////        for (String str : dataBaseFrom.keySet()) {
////            System.out.println(str+"  "+dataBaseFrom.get(str).toString());
////        }
//
////        ArrayList<String> strings = new ArrayList<String>();
////        strings.add("bgyf_courtyard");
////        strings.add("BGYF_BUILDING");
////        strings.add("BGYF_FLOOR");
////        strings.add("BGYF_APROOM");
////        strings.add("BGYF_STAFFDETAIL");
////        Map<String, DatabaseFrom> dataBaseFroms = databaseConnect.findDataBaseFroms("K8S", strings, "192.168.10.15:54321/ibasem_jgswj", "ibasem_bgyf", "south");
////        for (String str : dataBaseFroms.keySet()) {
////            System.out.println(str+"  "+dataBaseFroms.get(str).toString() +"\n\n\n");
////        }
//
////        Set<String> stringsdm = new HashSet<String>();
////        stringsdm.add("bgyf_courtyard");
////        stringsdm.add("BGYF_BUILDING");
////        stringsdm.add("BGYF_FLOOR");
////        stringsdm.add("BGYF_APROOM");
////        stringsdm.add("BGYF_STAFFDETAIL");
////        Map<String, DatabaseFrom> dataBaseFroms = databaseConnect.findDataBaseFroms("DM", stringsdm, "14.18.82.151:5236/IBASEM_BGYF", "IBASEM_BGYF", "southgissz");
////        for (String str : dataBaseFroms.keySet()) {
////            System.out.println(str+"  "+dataBaseFroms.get(str).toString() +"\n\n\n");
////        }
//
//    }
}
